﻿@page "/dashboard/configuration/chart/{panelId}/{dashboardId}/{serviceName?}/{instanceName?}/{endpointName?}"
@inherits TscComponentBase

<PageTitle>@I18n.Dashboard("Configure Chart")</PageTitle>

@if (Panel is not null)
{
    <ChartPanelConfiguration Value="Panel" ConfigurationRecord="ConfigurationRecord" />
}

@code {
    bool _findPanelFailed;

    [Inject]
    DashboardConfigurationRecord ConfigurationRecord { get; set; }

    [Inject]
    public NavigationManager NavigationManager { get; set; }

    [Parameter]
    public string DashboardId { get; set; }

    [Parameter]
    public string? ServiceName { get; set; }

    [Parameter]
    public string? InstanceName { get; set; }

    [Parameter]
    public string? EndpointName { get; set; }

    [Parameter]
    public string PanelId { get; set; }

    UpsertChartPanelDto? Panel { get; set; }

    protected override async Task OnInitializedAsync()
    {
        ConfigurationRecord.DashboardId = DashboardId;
        ConfigurationRecord.Service = ServiceName;
        ConfigurationRecord.Instance = InstanceName;
        ConfigurationRecord.Endpoint = EndpointName;
        ConfigurationRecord.PanelId = PanelId;
        if (ConfigurationRecord.Panels.Any() is false)
        {
            await ConfigurationRecord.BindPanelsAsync(ApiCaller);            
        }
        Panel = FindPanel(ConfigurationRecord.Panels) as UpsertChartPanelDto;
        _findPanelFailed = Panel is null;
    }

    protected override async Task OnParametersSetAsync()
    {
        if (string.IsNullOrEmpty(DashboardId) is false && ConfigurationRecord.DashboardId != DashboardId)
        {
            ConfigurationRecord.DashboardId = DashboardId;
            await ConfigurationRecord.BindPanelsAsync(ApiCaller);
            _findPanelFailed = ConfigurationRecord.Panels.Any() is false;
        }
        if (string.IsNullOrEmpty(PanelId) is false && ConfigurationRecord.PanelId != PanelId)
        {
            ConfigurationRecord.PanelId = PanelId;
            Panel = FindPanel(ConfigurationRecord.Panels) as UpsertChartPanelDto;
            _findPanelFailed = Panel is null;
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (_findPanelFailed)
        {
            _findPanelFailed = false;
            await OpenConfirmDialog(I18n.Dashboard("This chart does not exist, it will jump back to the dashboard configuration page"));
            ConfigurationRecord.IsEdit = true;
            ConfigurationRecord.NavigateToConfiguration();
        }
    }

    UpsertPanelDto? FindPanel(List<UpsertPanelDto> panels)
    {
        foreach (var panel in panels)
        {
            if (panel.Id == Guid.Parse(PanelId!))
            {
                return panel;
            }
            if (panel.ChildPanels.Any())
            {
                var data = FindPanel(panel.ChildPanels);
                if (data is not null) return data;
            }
        }

        return default;
    }
}
